package com.clss.commandhelper.tools.janus.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class DoTask extends TimerTask {
	Context context;
	public DoTask(Context context){
		this.context=context;
	}
	public void startAlarm(){
		//操作：发送一个广播，广播接收后Toast提示定时操作完成
		alarmreceiver alarmreceiver=new alarmreceiver();
	    Intent intent =new Intent(context,alarmreceiver.class);
	   intent.setAction("short");
	   PendingIntent sender=
	       PendingIntent.getBroadcast(context, 0, intent, 0);
	   
	   //设定一个五秒后的时间
	   Calendar calendar=Calendar.getInstance();
	   calendar.setTimeInMillis(System.currentTimeMillis());
	   calendar.add(Calendar.SECOND, 10);
	   
	   AlarmManager alarm=(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
	   alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
	   //或者以下面方式简化
	   //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
	   
	   Toast.makeText(context, "五秒后alarm开启", Toast.LENGTH_LONG).show();
	   IntentFilter logTaskFilter = new IntentFilter();
	    logTaskFilter.addAction("short");
	   context.registerReceiver(alarmreceiver, logTaskFilter);
	   
	}
	
	
   public static class alarmreceiver extends BroadcastReceiver{

       @Override
       public void onReceive(Context context, Intent intent) {
           // TODO Auto-generated method stub
           if(intent.getAction().equals("short")){
               Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show();
           }else{
               Toast.makeText(context, "repeating alarm",                       Toast.LENGTH_LONG).show();
           }
       }
   }
	
	// true时使用后台进程线程。只要剩下的程序记叙运行，后台进程线程就会执行。
	Timer myTimer;
	public void start(int delay, int hour) {
		myTimer = new Timer();
		myTimer.schedule(this, delay * 1000, hour*1000*60*60); //利用timer.schedule方法

		//public void schedule(TimerTask task,long time,long period)
		//task被安排在延迟time后执行，执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步，所以该方
	}

	public void startHour(Date time, long hour) {
		myTimer = new Timer();
		myTimer.schedule(this, time, hour*1000*60*60); //利用timer.schedule方法

		//public void schedule(TimerTask task,Date time,long period)
		//task被安排在time指定的时间执行，执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步，所以该方
	}
	public void startSec(long sec) {
		myTimer = new Timer();
		myTimer.schedule(this, sec*1000); //利用timer.schedule方法

		//public void schedule(TimerTask task,Date time,long period)
		//task被安排在time指定的时间执行，执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步，所以该方
	}
	public synchronized void run() {
		//执行任务(sql)
		System.out.println("do Task...");
		end();
		playstatus.onStatus("end");
	}

	public void end(){
		if(myTimer!=null)
		myTimer.cancel();
		//终止Timer的功能执行，但不会对正在执行的任务有影响。当执行cancel方法后将不能再用其分配任务。
	}
	public void diff(String startData,String endData){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		try
		{
		    Date d1 = df.parse(startData);
		    Date d2 = df.parse(endData);
		    long diff = d2.getTime() - d1.getTime();
		    long days = diff / (1000 * 60 * 60 * 24);
		    long hour = diff / (1000 * 60 * 60);
		    long min = diff / (1000 * 60);
		    long sec = diff / (1000);
		    System.out.println("diff "+diff);
		    System.out.println("days "+days);
		    System.out.println("hour "+hour);
		    System.out.println("min "+min);
		    System.out.println("sec "+sec);
		    
		    startSec(sec);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	Playstatus playstatus;
	public interface Playstatus{
		public void onStatus(String status);
	}
	public void setOnPlaystatus(Playstatus playstatus){
		this.playstatus=playstatus;
	}
}